import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '浮游测试',
      theme: ThemeData(
        primarySwatch: Colors.pink,
        primaryColor: Colors.cyan,
        iconTheme: IconThemeData(color: Colors.yellow),
      ),
      home: MyHomePage(title: '哈哈哈哈12'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int count = 0;

  void _increment() => setState(() {
        count++;
      });
  @override
  Widget build(BuildContext context) {
    return CountContainer(model: this, increment: _increment, child: Counter());
  }
}

class CountContainer extends InheritedWidget {
  static CountContainer of(BuildContext context) =>
      context.dependOnInheritedWidgetOfExactType();

  final _MyHomePageState model;
  final Function() increment;

  CountContainer({
    Key key,
    @required this.model,
    @required this.increment,
    @required Widget child,
  }) : super(key: key, child: child);

  @override
  bool updateShouldNotify(CountContainer oldWidget) =>
      model != oldWidget.model;
}

class Counter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CountContainer state = CountContainer.of(context);
    return Scaffold(
      appBar: AppBar(title: Text('嘻嘻☺️')),
      body: Text(
          'You have pushed the button this many times: ${state.model.count}'),
      floatingActionButton: FloatingActionButton(onPressed: state.increment),
    );
  }
}
